// components/calendar/calendar.js
const { formatTime } = require("../../utils/util")
Component({
	properties: {
		taskDateList: Array,
		showModalStatus: Boolean,
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		dateList: [], //日历主体渲染数组
		selectDay: {}, //选中时间
		diaryList: [],
		startDate: '',
		endDate: ''
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		//picker设置月份
		editMonth(e) {
			const arr = e.detail.value.split("-")
			const year = parseInt(arr[0])
			const month = parseInt(arr[1])
			let length = this.data.taskDateList.length
			if(length === 0){
				return
			}
			let startDate = this.data.taskDateList[0].taskDate
			let dateStrList = startDate.split('-')
			let [startYear,startMonth] = dateStrList
			let endDate = this.data.taskDateList[length-1].taskDate
			let dateStrList1 = endDate.split('-')
			let [endYear,endMonth] = dateStrList1
			if(year < Number(startYear) || year > Number(endYear)){
				wx.showToast({
					title: '超出训练周期，不能切换',
					icon: 'none'
				})
				return
			}
			if(year === Number(startYear) ){
				if(month < Number(startMonth)){
					wx.showToast({
						title: '超出训练周期，不能切换',
						icon: 'none'
					})
					return
				}
			}
			if(year === Number(endYear) ){
				if(month > Number(endMonth)){
					wx.showToast({
						title: '超出训练周期，不能切换',
						icon: 'none'
					})
					return
				}
			}
			this.setMonth(year, month)
		},
		//上月切换按钮点击
		lastMonth() {
			const lastMonth = new Date(this.data.selectDay.year, this.data.selectDay.month - 2)
			const year = lastMonth.getFullYear()
			const month = lastMonth.getMonth() + 1
			let length = this.data.taskDateList.length
			if(length === 0){
				return
			}
			let startDate = this.data.taskDateList[0].taskDate
			let dateStrList = startDate.split('-')
			let [startYear,startMonth] = dateStrList
			if(year < Number(startYear)){
				wx.showToast({
					title: '超出训练周期，不能切换',
					icon: 'none'
				})
				return
			}

			if(year === Number(startYear)){
				if(month < Number(startMonth)){
					wx.showToast({
						title: '超出训练周期，不能切换',
						icon: 'none'
					})
					return
				}
			}
			
			this.setMonth(year, month)
		},
		//下月切换按钮点击
		nextMonth() {
			const nextMonth = new Date(this.data.selectDay.year, this.data.selectDay.month)
			const year = nextMonth.getFullYear()
			const month = nextMonth.getMonth() + 1
			let length = this.data.taskDateList.length
			if(length === 0){
				return
			}
			let endDate = this.data.taskDateList[length-1].taskDate
			let dateStrList = endDate.split('-')
			let [endYear,endMonth] = dateStrList
			if(year > Number(endYear)){
				wx.showToast({
					title: '超出训练周期，不能切换',
					icon: 'none'
				})
				return
			}
			if(year === Number(endYear)){
				if(month > Number(endMonth)){
					wx.showToast({
						title: '超出训练周期，不能切换',
						icon: 'none'
					})
					return
				}
			}
			this.setMonth(year, month)
		},
		//设置月份
		setMonth(setYear, setMonth, setDay) {
			if (this.data.selectDay.year !== setYear || this.data.selectDay.month !== setMonth) {
				const day = Math.min(new Date(setYear, setMonth, 0).getDate(), this.data.selectDay.day)
				const time = new Date(setYear, setMonth - 1, setDay ? setDay : day)
				const data = {
					selectDay: {
						year: setYear,
						month: setMonth,
					}
				}
				this.setData(data)
				this.getDateList()
			}
		},

		handleChange: function(){
			setTimeout(()=>{
				this.triggerEvent("change", this.data.chooseDay)
			},100)
		},
		//日历主体的渲染方法
		dateInit() {
			this.getDateList()
			this.handleChange()
		},
		getDateList(){
			const {
				year,
				month,
				day
			} = this.data.selectDay
			let {
				chooseDay,
				taskDateList
			} = this.data

			let dateList = []; //需要遍历的日历数组数据

			let now = new Date(year, month - 1) //当前月份的1号
			let startWeek = now.getDay(); //目标月1号对应的星期
			let dayNum = new Date(year, month, 0).getDate() //当前月有多少天
			let forNum = Math.ceil((startWeek + dayNum) / 7) * 7 //当前月跨越的周数

			let length = this.data.taskDateList?.length
			let startDate = this.data.taskDateList[0].taskDate
			let endDate = this.data.taskDateList[length-1].taskDate
		
			for (let i = 0; i < forNum; i++) {
				const now2 = new Date(now)
				const day = now2.getDate()
				now2.setDate(i - startWeek + 1)
				let obj = {};
				obj = {
					day: now2.getDate(),
					month: now2.getMonth() + 1,
					year: now2.getFullYear(),
					hasDiary: false,
					isVacant:false,
					isSelect: false,
					dateString: formatTime(now2, "Y-M-D")
				};
				dateList[i] = obj;
			}

			dateList.map((item, index) => {
				item.dateString !== chooseDay ? item.isSelect = false : item.isSelect = true
				return item
			})

			for (const item of dateList) {
				for (const pathItem of taskDateList) {
					const releaseDay = pathItem.taskDate
					if (releaseDay === item.dateString) {
						item.hasDiary = true
						if(pathItem.isVacant === 1){
							item.isVacant = true
						}
					}
				}
			}

			this.setData({
				dateList: dateList,
				startDate: startDate,
				endDate: endDate
			})
		},
		//一天被点击时
		selectChange(e) {
			const {
				year,
				month,
				day,
				dateString,
				index
			} = e.currentTarget.dataset

			const selectDay = {
				year: year,
				month: month,
				dateString: dateString
			}

			let length = this.data.taskDateList?.length

			if(length === 0){
				return
			}
			let startDate = this.data.taskDateList[0].taskDate
			let dateStrList = startDate.split('-')
			let [startYear,startMonth,startDay] = dateStrList
			let endDate = this.data.taskDateList[length-1].taskDate
			let dateStrList1 = endDate.split('-')
			let [endYear,endMonth,endDay] = dateStrList1
			if(year < Number(startYear) || year > Number(endYear)){
				wx.showToast({
					title: '日期不在训练周期内',
					icon: 'none'
				})
				return
			}
			if(year === Number(startYear) ){
				if(month < Number(startMonth)){
					wx.showToast({
						title: '日期不在训练周期内',
						icon: 'none'
					})
					return
				}
				if(month === Number(startMonth)){
					if(day < Number(startDay)){
						wx.showToast({
							title: '日期不在训练周期内',
							icon: 'none'
						})
						return
					}
				}
			}
			if(year === Number(endYear) ){
				if(month > Number(endMonth)){
					wx.showToast({
						title: '日期不在训练周期内',
						icon: 'none'
					})
					return
				}
				if(month === Number(endMonth)){
					if(day > Number(endDay) ){
						wx.showToast({
							title: '日期不在训练周期内',
							icon: 'none'
						})
						return
					}
				}
			}
			this.setData({
				selectDay: selectDay,
				chooseDay: selectDay.dateString,
				chooseIndex: index
			})

			if (this.data.selectDay.year !== year || this.data.selectDay.month !== month) {
				this.setMonth(year, month, '')
			} else if (this.data.selectDay.day !== day) {
			}

			this.getDateList()
			this.handleChange()
		}
	},
	lifetimes: {
		attached() {
			let now = this.data.today ? new Date(this.data.today) : new Date()
			let selectDay = {
				year: now.getFullYear(),
				month: now.getMonth() + 1,
				day: now.getDate(),
				dateString: formatTime(now, "Y-M-D")
			}
			this.setData({
				selectDay,
				chooseDay: selectDay.dateString
			})
			this.dateInit(selectDay.year, selectDay.month, selectDay.day)
		}
	},

})
